require(["common", "jQuery", "coAjax", "queryString", "href", "hash_routie", "moment"], function(jSouper, $, coAjax, QueryString, href, hash_routie, moment) {
	//INIT
	var LOTTERY_TYPE_array = [];
	jSouper.forEach(appConfig.LOTTERY_TYPE, function(value, key) {
		LOTTERY_TYPE_array.push({
			key: key,
			value: value
		});
	});
	App.set("$Cache.LOTTERY_TYPE_array", LOTTERY_TYPE_array);
	var LOTTERY_STATE_array = [];
	jSouper.forEach(appConfig.LOTTERY_STATE, function(value, key) {
		LOTTERY_STATE_array.push({
			key: key,
			value: value
		});
	});
	App.set("$Cache.LOTTERY_STATE_array", LOTTERY_STATE_array);

	var queryString;
	hash_routie.on("lottery", function() {
		var bus_id = App.get("bus_loginer._id");
		coAjax.get(appConfig.bus_lottery_list, {
			bus_id: bus_id
		}, function(result) {
			App.set("$Cache.lottery.list", result.result)
		});
		queryString = new QueryString(location.hash);

		var lottery_id = queryString.get("lottery_id");
		if (lottery_id) {
			coAjax.get(appConfig.bus_lottery_by_id + lottery_id, {
				bus_id: bus_id
			}, function(result) {
				App.set("$Cache.lottery.current_lottery", result.result)
			});
		} else {
			App.set("$Cache.lottery.current_lottery", {
				awards: []
			})
		}
	});

	App.set("$Event.lottery.add_lottery", function() {
		var lottery_info = App.get("$Cache.lottery.current_lottery")
		coAjax.post(appConfig.bus.extend.lottery.add_lottery, lottery_info, function(result) {
			hash_routie.hash({
				lottery_id: result.result._id
			});
			alert("success", "抽奖活动添加成功")
		});
	});
	App.set("$Event.lottery.current_lottery_add_award", function() {
		App.push("$Cache.lottery.current_lottery.awards", {});
	});
	App.set("$Event.lottery.current_lottery_remove_award", function(e, vm) {
		// var _index = vm.get("$Index");
		// var awards = App.get("$Cache.lottery.current_lottery.awards");
		// awards.splice(_index, 1);
		// App.set("$Cache.lottery.current_lottery.awards", awards);
		vm.removeFromArray();
	});
	App.set("$Event.lottery.to_update_lottery", function(e, vm) {
		hash_routie.hash({
			lottery_id: vm.get("_id")
		});
	});
	App.set("$Event.lottery.update_lottery", function(e, vm) {
		var lottery_info = App.get("$Cache.lottery.current_lottery");
		coAjax.put(appConfig.bus.extend.lottery.update_lottery + lottery_info._id, lottery_info, function(result) {
			hash_routie.emit("lottery");
			alert("success", "抽奖活动修改成功")
		});
	});
	App.set("$Event.lottery.cancel_update_lottery", function() {
		hash_routie.hash({
			lottery_id: ""
		});
	});
	App.set("$Event.lottery.remove_lottery", function(e, vm) {
		var lottery_info = vm.get();
		myConfirm("确定要移除“" + lottery_info.title + "”这个抽奖活动吗？", function() {
			coAjax["delete"](appConfig.bus.extend.lottery.remove_lottery + lottery_info._id, function(result) {
				App.set("$Cache.lottery.list", result.result)
				if (lottery_info._id == queryString.get("lottery_id")) {
					hash_routie.hash({
						lottery_id: ""
					});
				}
				alert("success", "抽奖活动删除成功")
			});
		});
	});
	App.set("$Event.lottery.toggle_winner_list", function(e, vm) {
		var _show_winner_list = !vm.get("$Private.show_winner_list");
		vm.set("$Private.show_winner_list", _show_winner_list);
		if (_show_winner_list) {
			coAjax.get(appConfig.bus.extend.lottery.get_lottery_with_winner_list_info_by_id + vm.get("_id"), function(result) {
				var lottery_info = result.result
				vm.set(lottery_info);
				var _total_len = 0
				jSouper.forEach(lottery_info.awards, function(_awards_item) {
					_total_len += _awards_item.winner_list_info.length
				});
				vm.set("$Private.total_winner_length",_total_len);
			});
		}
	});

});